/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package form.input;


import br.entidade.business.Business;
import br.entidade.sft.SftInputUnid;
import form.JFPrincipal;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import superior.mensagens.ErroMensagens;

/**
 *
 * @author Adriano
 */
public class JIUnidade extends javax.swing.JInternalFrame {

    /**
     * Creates new form JIUnid
     */
    public JIUnidade() {
        initComponents();
        iniciarComponentes();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel1 = new javax.swing.JPanel();
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        unidIdIdunidLabel = new javax.swing.JLabel();
        unidNmNmunidLabel = new javax.swing.JLabel();
        unidCodCodigoLabel = new javax.swing.JLabel();
        unidIdIdunidField = new javax.swing.JTextField();
        unidNmNmunidField = new javax.swing.JTextField();
        unidCodCodigoField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        unidMtMultiploCB = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        shareFild = new javax.swing.JTextField();
        jLabel = new javax.swing.JLabel();
        shareButton = new javax.swing.JButton();

        setTitle("Unidade");
        setMaximumSize(new java.awt.Dimension(448, 398));
        setMinimumSize(new java.awt.Dimension(448, 398));
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(448, 398));

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setEnabled(false);
        jPanel1.setMaximumSize(new java.awt.Dimension(428, 287));
        jPanel1.setMinimumSize(new java.awt.Dimension(428, 287));
        jPanel1.setPreferredSize(new java.awt.Dimension(428, 287));

        masterTable.setModel(dtm);
        masterTable.setSelectionMode(ListSelectionM­odel.MULTIPLE_INTERVAL_SELECTION);
        lsm = masterTable.getSelectionModel();
        lsm.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    jTTabelaLinhaSelecionada(masterTable);
                }
            }
        });
        masterScrollPane.setViewportView(masterTable);
        masterTable.getColumnModel().getColumn(0).setPreferredWidth(40);

        unidIdIdunidLabel.setText("ID:");

        unidNmNmunidLabel.setText("Nome:");

        unidCodCodigoLabel.setText("Codigo:");

        unidIdIdunidField.setEditable(false);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), unidIdIdunidField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), unidNmNmunidField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        unidNmNmunidField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                unidNmNmunidFieldKeyReleased(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), unidCodCodigoField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        unidCodCodigoField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                unidCodCodigoFieldKeyReleased(evt);
            }
        });

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/botoes/save.png"))); // NOI18N
        saveButton.setToolTipText("Salvar");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        refreshButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/botoes/refresh.png"))); // NOI18N
        refreshButton.setToolTipText("Atualizar");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        newButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/botoes/new.png"))); // NOI18N
        newButton.setToolTipText("Novo");
        newButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newButtonActionPerformed(evt);
            }
        });

        deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/botoes/Delete.png"))); // NOI18N
        deleteButton.setToolTipText("Deletar");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/botoes/exit.png"))); // NOI18N
        jButton1.setToolTipText("Sair");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        unidMtMultiploCB.setText("Unidade Multiplas");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), unidMtMultiploCB, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        unidMtMultiploCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unidMtMultiploCBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(unidMtMultiploCB)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(unidIdIdunidLabel)
                            .addComponent(unidNmNmunidLabel)
                            .addComponent(unidCodCodigoLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(unidNmNmunidField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(unidIdIdunidField)
                            .addComponent(unidCodCodigoField)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(newButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(refreshButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(masterScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(unidIdIdunidLabel)
                    .addComponent(unidIdIdunidField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(unidNmNmunidLabel)
                    .addComponent(unidNmNmunidField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(unidCodCodigoLabel)
                    .addComponent(unidCodCodigoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(unidMtMultiploCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(refreshButton)
                    .addComponent(deleteButton)
                    .addComponent(newButton)
                    .addComponent(jButton1))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        shareFild.setNextFocusableComponent(shareButton);

        jLabel.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel.setText("Nome/Cod:");

        shareButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/botoes/search.png"))); // NOI18N
        shareButton.setToolTipText("Pesquisar");
        shareButton.setNextFocusableComponent(masterScrollPane);
        shareButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                shareButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(shareFild)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(shareButton)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(shareButton)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(shareFild, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        
        try {
            dao.salvar();
        } catch (RollbackException e) {
            dao.getEntityManager().getTransaction().begin();
            dao.refresh();
            list = dao.findAll(SftInputUnid.class);
            ErroMensagens.getInstancia().ErroMensagem("Você deve estar cometendo um dos seguntes erros. \n"
                                                    + "1 - Unidade com nome vazio. \n"
                                                    + "2 - Unidade com Cod vazio. \n"
                                                    + "3 - Unidade já cadastrado. ");;
            System.err.println("Column 'unid_nm_nmunid' cannot be null ");
        }
        mostraPesquisa();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        
        dao.refresh();
        list = dao.findAll(SftInputUnid.class);
        mostraPesquisa();
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        br.entidade.sft.SftInputUnid s = new br.entidade.sft.SftInputUnid();
        dao.criar(s);
        list.add(s);
        mostraPesquisa();
        int row = list.size() - 1;
        masterTable.setRowSelectionInterval(row, row);
        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
    }//GEN-LAST:event_newButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int[] selected = masterTable.getSelectedRows();
        List<br.entidade.sft.SftInputUnid> toRemove = new ArrayList<>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            br.entidade.sft.SftInputUnid s = list.get(masterTable.convertRowIndexToModel(selected[idx]));
            toRemove.add(s);
            dao.remover(s);
        }
        list.removeAll(toRemove);
        mostraPesquisa();
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        dao.getEntityManager().close();
        JFPrincipal.setU(null);
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void unidNmNmunidFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_unidNmNmunidFieldKeyReleased
        // TODO add your handling code here:
        unidNmNmunidField.setText(unidNmNmunidField.getText().toUpperCase());
        list.get(masterTable.getSelectedRow()).setUnidNmNmunid(unidNmNmunidField.getText());
        masterTable.setValueAt(unidNmNmunidField.getText(), masterTable.getSelectedRow(), 1);
    }//GEN-LAST:event_unidNmNmunidFieldKeyReleased

    private void unidCodCodigoFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_unidCodCodigoFieldKeyReleased
        // TODO add your handling code here:
        try {
            list.get(masterTable.getSelectedRow()).setUnidCodCodigo(Integer.valueOf(unidCodCodigoField.getText()));
            masterTable.setValueAt(unidCodCodigoField.getText(), masterTable.getSelectedRow(), 2);
        } catch (NumberFormatException e) {
            ErroMensagens.getInstancia().PerigoMensagem("Este campo recebe apenas numeros!");
            unidCodCodigoField.setText(String.valueOf(list.get(masterTable.getSelectedRow()).getUnidCodCodigo()));
            masterTable.setValueAt(unidCodCodigoField.getText(), masterTable.getSelectedRow(), 2);
        }
    }//GEN-LAST:event_unidCodCodigoFieldKeyReleased

    private void shareButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shareButtonActionPerformed
        // TODO add your handling code here:
        try {
            list = dao.getEntityManager().createNamedQuery("SftInputUnid.findByUnidCodCodigo",SftInputUnid.class)
                    .setParameter("unidCodCodigo", Integer.parseInt(shareFild.getText())).getResultList();
        } catch (NumberFormatException n) {
            list = dao.getEntityManager().createNamedQuery("SftInputUnid.findByUnidNmNmunid",SftInputUnid.class)
                    .setParameter("unidNmNmunid", "%" + shareFild.getText() + "%").getResultList();
        }
        mostraPesquisa();
    }//GEN-LAST:event_shareButtonActionPerformed

    private void unidMtMultiploCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unidMtMultiploCBActionPerformed
        // TODO add your handling code here:
        list.get(masterTable.getSelectedRow()).setUnidMtMultiplo(unidMtMultiploCB.isSelected());
        masterTable.setValueAt(unidMtMultiploCB.isSelected(), masterTable.getSelectedRow(), 3);
    }//GEN-LAST:event_unidMtMultiploCBActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JButton newButton;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton shareButton;
    private javax.swing.JTextField shareFild;
    private javax.swing.JTextField unidCodCodigoField;
    private javax.swing.JLabel unidCodCodigoLabel;
    private javax.swing.JTextField unidIdIdunidField;
    private javax.swing.JLabel unidIdIdunidLabel;
    private javax.swing.JCheckBox unidMtMultiploCB;
    private javax.swing.JTextField unidNmNmunidField;
    private javax.swing.JLabel unidNmNmunidLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    private List<SftInputUnid> list;
    private final DefaultTableModel dtm = new DefaultTableModel(null, new String[]{"Id", "Nome", "Codigo", "Unidade Multipla"});
    private ListSelectionModel lsm;
    private Business dao;

    private void iniciarComponentes() {
        dao = new Business();
        dao.getEntityManager().getTransaction().begin();
        listarPesquisa();
    }

    private void listarPesquisa() {

        list = dao.findAll(SftInputUnid.class);
        mostraPesquisa();
    }

    private void mostraPesquisa() {
        removeDesativado();
        while (dtm.getRowCount() > 0) {
            dtm.removeRow(0);
        }
        if (list.isEmpty()) {
            ErroMensagens.getInstancia().InformacaoMensagem("Nenhum resultado encontrado");
        } else {

            String[] linha = new String[]{null, null, null, null};
            for (int i = 0; i < list.size(); i++) {
                dtm.addRow(linha);
                dtm.setValueAt(list.get(i).getUnidIdIdunid(), i, 0);
                dtm.setValueAt(list.get(i).getUnidNmNmunid(), i, 1);
                dtm.setValueAt(list.get(i).getUnidCodCodigo(), i, 2);
                dtm.setValueAt(list.get(i).isUnidMtMultiplo(), i, 3);
            }
        }
    }

    public void jTTabelaLinhaSelecionada(JTable masterTable) {
        if (masterTable.getSelectedRow() != -1) {
            unidIdIdunidField.setText(String.valueOf(list.get(masterTable.getSelectedRow()).getUnidIdIdunid()));
            unidNmNmunidField.setText(list.get(masterTable.getSelectedRow()).getUnidNmNmunid());
            unidCodCodigoField.setText(String.valueOf(list.get(masterTable.getSelectedRow()).getUnidCodCodigo()));
            unidMtMultiploCB.setSelected(list.get(masterTable.getSelectedRow()).isUnidMtMultiplo());
        } else {
            unidIdIdunidField.setText("");
            unidNmNmunidField.setText("");
            unidCodCodigoField.setText("");
            unidMtMultiploCB.setSelected(false);
        }
    }
    
    private void removeDesativado() {
        List<SftInputUnid> toRemove = new ArrayList<>();
        for (SftInputUnid sftUnid : list) {
            try{ if (sftUnid.getUnidNmNmunid().contains("ZZ ")) toRemove.add(sftUnid);}catch(NullPointerException e){}
        }
        list.removeAll(toRemove);
    }

}
